package si.sp.socialnetwork;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;


public class DatabaseBean 
{

	private ArrayList<User> users; 
	private User user;
        private Slika slika;
        private Objava objava;
	private DatabaseDAO dao;
	private ResultSet results;
	private boolean success;
	
	private ArrayList<String> krogi;
        private ArrayList<Objava> objave;
        private ArrayList<Slika> slike;
        private HashMap<String, String> session_attributes;
        private ArrayList<User> matchingUsers;


	public boolean setUser(User user)
	{
		try {
			dao = new DatabaseDAO();
			dao.insertUser(user);
			success = true;

		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
			success = false;
		} finally {
			dao.closeConnection(); 
		}   

		return success;
	}

	public ArrayList<User> getUsers()
	{
		try {
			dao = new DatabaseDAO();
			results = dao.getUser();
			users = new ArrayList<User>();

			while( results.next() ) {
				user = new User(
                        	results.getInt("IDracun"),
						results.getString("email"),
						results.getString("geslo"),
						results.getString("ime"),
						results.getString("priimek"),
						results.getString("kraj"),
						results.getString("spol"),
						results.getDate("rojstni_datum")
						);
				users.add(user);
			}
		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return users;
	}
        
        //za iskalnik prijateljev
        public ArrayList<User> getMatchingUsers(String name)
	{
		try {
			dao = new DatabaseDAO();
			results = dao.getMatchingUser(name);
			matchingUsers = new ArrayList<User>();

			while( results.next() ) {
				user = new User( 
                        	results.getInt("IDracun"),
						results.getString("email"),
						results.getString("geslo"),
						results.getString("ime"),
						results.getString("priimek"),
						results.getString("kraj"),
						results.getString("spol"),
						results.getDate("rojstni_datum")
						);
				matchingUsers.add(user);
			}
		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return matchingUsers;
	}
        
        public ArrayList<Objava> getObjave(int id){
            try{
                dao = new DatabaseDAO();
                results = dao.getObjave(id);
                objave = new ArrayList<Objava>();
                
                while( results.next() ) {
				objava = new Objava( 
						results.getString("ime"),
						results.getString("priimek"),
						results.getString("spletna_stran"),
						results.getString("vsebina"),
						results.getString("ime_kroga")						
						);
				objave.add(objava);
			}
		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return objave;
	}
        
        public ArrayList<Slika> getSlike(int id){
            try{
                dao = new DatabaseDAO();
                results = dao.getObjave(id);
                slike = new ArrayList<Slika>();
                
                while( results.next() ) {
				slika = new Slika( 
						results.getString("ime"),
						results.getString("priimek"),
						results.getString("url"),
						results.getString("opis"),
						results.getString("ime_kroga")						
						);
				slike.add(slika);
			}
		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return slike;
	}
        
	public ArrayList<String> getKrogiForUserID(int id)
	{
		try {
			dao = new DatabaseDAO();
			results = dao.getKrogiForUserID(id);
			krogi = new ArrayList<String>();
			String krog = new String();

			while( results.next() ) {
				krog = results.getString("ime_kroga");
				krogi.add(krog);
			}
		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return krogi;
	}
        
        public boolean isAccessGranted(String un, String psw)
	{
		boolean access_granted = false;
		
		try {
			dao = new DatabaseDAO();
			results = dao.getMatchingUsers(un, psw);
			
			session_attributes = new HashMap<String, String>();

			int count = 0;
			while(results.next()) {
				session_attributes.put("userID", results.getString("IDracun"));
				session_attributes.put("user", results.getString("email"));
				
				count++;
			}
			if(count > 0) {
				access_granted = true;
			}


		} catch (SQLException ex) {
			Logger.getLogger(DatabaseBean.class.getName()).log(Level.SEVERE, null, ex);
		} finally   {
			dao.closeConnection(); 
		}
		return access_granted;
	}
	
	public HashMap<String, String> getSessionAttributes() {
		return session_attributes;
	}
}
